package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 衷岚
 * @version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //封装到实体类中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        Long setmealId = shoppingCartDTO.getSetmealId();
        Long dishId = shoppingCartDTO.getDishId();
        Long userId = BaseContext.getCurrentId();

        //判断添加的是菜品还是套餐
        if(dishId != null){
            //本次添加的是菜品

            //判断商品是否加购过 菜品口味不一样的也算不同数据
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

            //加购过
            if(list!=null && list.size()>0){
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()+1);
                shoppingCartMapper.update(cart);
            }else{
            //没加购过
                //查询菜品相关信息插入到购物车数据中
                Dish dish = dishMapper.findById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);

                shoppingCartMapper.add(shoppingCart);

            }
        }else{
            //本次添加的是套餐

            //是否加购过
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

            //加购过
            if (list != null && list.size() > 0) {
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()+1);
                shoppingCartMapper.update(cart);
            }else{
            //没加购过
                Setmeal setmeal = setmealMapper.findById(setmealId);

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);

                shoppingCartMapper.add(shoppingCart);
            }
        }

    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        List<ShoppingCart> cartList = shoppingCartMapper.list(cart);
        return cartList;
    }

    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();

        //判断是菜品还是套餐
        //判断商品数量
        // 大于等于2就减少数量
        // 为1就删除这条商品数据

        //1.菜品
        if(dishId != null){
            //获取菜品数量信息
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart cart = list.get(0);

            if(cart.getNumber()>=2){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.update(cart);
            }else{
                shoppingCartMapper.delete(cart);
            }
        }

        //2.套餐
        if(setmealId != null){
            //获取套餐数量信息
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart cart = list.get(0);
            if(cart.getNumber()>=2){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.update(cart);
            }else{
                shoppingCartMapper.delete(cart);
            }

        }

    }
}
